fix: validate usePluginData return type against default value - #46
Conversation
The Go backend returns `any` from the data store, and the TypeScript generic cast (`result as T`) provides no runtime safety. When stored data has a different JSON type than expected (e.g. object instead of array), callers crash with confusing errors like "favorites.includes is not a function". Add a `matchesShape` check that validates the structural type (array, object, primitive) matches the default value before accepting the stored result. Falls back to the default with a console warning on mismatch.
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughAdded runtime type validation to the Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Tip CodeRabbit can use Trivy to scan for security misconfigurations and secrets in Infrastructure as Code files.Add a .trivyignore file to your project to customize which findings Trivy reports. |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/omniviewdev-runtime/src/hooks/data/usePluginData.ts`:
- Around line 17-23: The matchesShape function treats defaultValue === null as
an "object" because typeof null === 'object', causing non-null objects/arrays to
incorrectly match; update matchesShape to first check if defaultValue === null
and return value === null in that case so only null matches null, then keep the
existing array and object checks (referring to the matchesShape function) and
the final typeof comparison for primitives.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 02916349-22a0-46f8-aac5-abd31c354adc
📒 Files selected for processing (1)
packages/omniviewdev-runtime/src/hooks/data/usePluginData.ts
typeof null === 'object' caused null defaults to match any object or array. Check for null explicitly before the object/array branches.
Summary
usePluginDatabefore accepting stored valuesanyfrom the data store, and theresult as Tcast provides no runtime safety — when stored JSON has a different type than expected (e.g. object instead of array), callers crash with errors likefavorites.includes is not a functionmatchesShapehelper checks array vs object vs primitive alignment against the default value; mismatches fall back to the default with a console warningTest plan
cluster_favoriteskey has corrupt/mismatched data in the storeSummary by CodeRabbit